package com.blobfish.fiction.common.job;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.blobfish.fiction.common.pageprocessor.XicidailiPageProcessor;
import com.blobfish.fiction.common.pipeline.XicidailiPipeline;
import com.blobfish.fiction.common.util.InitPluginUtil;
import com.jfinal.kit.PropKit;
import us.codecraft.webmagic.Spider;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CallTougao {
    /**
     * 刷点击量
     */
    public void call() {
        Console.log("开始刷点击量啦！---------");
        String url = "http://tougao.12371.cn/gaojian.php?tid=1832050&from=timeline";
        Set<String> proxyIpSet = InitPluginUtil.getInstance().initRedisPlugin().smembers(PropKit.get("proxyIpList"));
        Iterator<String> proxyIpSetIt = proxyIpSet.iterator();
        while (proxyIpSetIt.hasNext()) {
            final String proxyIpStr = proxyIpSetIt.next();
            ThreadUtil.execAsync(
                    new Runnable() {
                        @Override
                        public void run() {
                            String[] proxyIpStrSplits = proxyIpStr.split(":");
                            int errNum = 0;
                            int successNum = 0;
                            HttpRequest httpRequest = HttpUtil.createRequest(Method.GET, url);
                            httpRequest.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIpStrSplits[0], Convert.toInt(proxyIpStrSplits[1]))));
                            httpRequest.timeout(3000);
                            while (errNum < 3 && successNum < 2) {
                                try {
                                    String listContent = httpRequest.execute().body();
                                    List<String> titles = ReUtil.findAll("<i class=\"time_ren_r\" style=\"float: right;margin:0px 10px 0px 0px; color: #666;\">(.*?)</i>", listContent, 1);
                                    titles.forEach(item -> Console.log(item));
                                    ++successNum;
                                } catch (Exception e) {
                                    ++errNum;
                                    Console.error("发生错误:{}", e.getMessage());
                                    if (errNum == 5) {
                                        proxyIpSetIt.remove();
                                        InitPluginUtil.getInstance().initRedisPlugin().srem(PropKit.get("proxyIpList"), proxyIpStr);
                                        break;
                                    }
                                }
                            }
                        }
                    });
        }
    }

    /**
     * 刷新代理池
     */
    public void refreshProxy() {
        Console.log("开始更新代理池！---------");
        Spider.create(new XicidailiPageProcessor())
                .addPipeline(new XicidailiPipeline())
                .addUrl("http://www.xicidaili.com/wn/1",
                        "http://www.xicidaili.com/wn/2",
                        "http://www.xicidaili.com/wn/3",
                        "http://www.xicidaili.com/wn/4",
                        "http://www.xicidaili.com/wn/5",
                        "http://www.xicidaili.com/wn/6",
                        "http://www.xicidaili.com/wn/7",
                        "http://www.xicidaili.com/wn/8",
                        "http://www.xicidaili.com/wn/9",
                        "http://www.xicidaili.com/wn/10",
                        "http://www.xicidaili.com/wt/1",
                        "http://www.xicidaili.com/wt/2",
                        "http://www.xicidaili.com/wt/3",
                        "http://www.xicidaili.com/wt/4",
                        "http://www.xicidaili.com/wt/5",
                        "http://www.xicidaili.com/wt/6",
                        "http://www.xicidaili.com/wt/7",
                        "http://www.xicidaili.com/wt/8",
                        "http://www.xicidaili.com/wt/9",
                        "http://www.xicidaili.com/wt/10").thread(10).run();
    }
}
